package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.date.DateUtils;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.OrdersettingMapper;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Autowired
    OrdersettingMapper ordersettingMapper;

    @Autowired
    MemberMapper memberMapper;

    @Override
    public Order addOrder(Map map) {
        String orderDate =(String) map.get("orderDate");
        String telephone =(String) map.get("telephone");
        String name =(String) map.get("name");
        String sex =(String) map.get("sex");
        String idCard =(String) map.get("idCard");
        String setmealId =(String) map.get("setmealId");
        String orderType =(String) map.get("orderType");
        System.out.println(orderDate);
        // 查询当日是否预约已满
        int count = ordersettingMapper.isOrderSettingAvailable(orderDate);
        if (count>0){
            // 预约未满 可以生成预约订单 在此之前先判断是老用户还是新用户,并保存信息
            Order order = new Order();
            order.setOrderDate(DateUtils.parseString2Date2(orderDate));
            order.setOrderType(orderType);
            order.setOrderStatus("未到诊");
            order.setSetmealId(Integer.parseInt(setmealId));
            QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PHONENUMBER",telephone);
            Member member = memberMapper.selectOne(queryWrapper);
            if (member==null){
                // 新用户
                Member newMember = new Member();
                newMember.setName(name);
                newMember.setSex(sex);
                newMember.setIdCard(idCard);
                newMember.setPhoneNumber(telephone);
                newMember.setRegTime(new Date());
                memberMapper.insert(newMember);
                order.setMemberId(newMember.getId());
            }else {
                // 老用户 (可能存在这一天已经预约过了 需要判断)
                int i = baseMapper.findOrderIsExist(member.getId(),orderDate,setmealId);
                if (i>0){
                    // 此预约订单已经存在
                    throw new RuntimeException("请勿重复预约");
                }else {
                    // 预约订单不存在
                    order.setMemberId(member.getId());
                }
            }
            baseMapper.insert(order);
            ordersettingMapper.addReservationsByOrderDate(orderDate);
            return order;
        }else {
            throw new RuntimeException("当天预约满了，请改期");
        }
    }

    @Override
    public Map findMemberNameAndSetmealNameAndOrderById(Integer id) {
        Map map = baseMapper.findMemberNameAndSetmealNameAndOrderById(id);
        return map;
    }
}
